package heima.lm.oschina.common;

import android.view.View;
import android.widget.FrameLayout;

import java.util.List;

import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;

/**
 * Created by herenxue on 2017/4/1.
 */

public abstract class CommonPager {

    public FrameLayout mRootView;
    public boolean isReadData;
    public boolean isNullData;
    public boolean isLoadedData;
    //判断是否有网络
    public boolean enableNetwork;
    private View mErrorView;
    private View mEmptyView;
    private View mNoNetView;
    private View mProgressView;

    public CommonPager() {
        mRootView = (FrameLayout) View.inflate(BaseApplication.mApp, R.layout.pager_common, null);
    }

    protected abstract void loadData();

    protected abstract void loadSuccessPager();

    public void dynamic() {
        loadProgressPager();
        loadData();
    }

    public void dealCommonFailure() {
        isReadData = false;
        runOnUiThread();
    }

    public void dealCommonSuccess(Object obj) {
        isReadData = true;
        if (obj instanceof List) {
            List list = (List) obj;
            isNullData = list.size() == 0;
        } else {
            isNullData = obj == null;
        }
        runOnUiThread();
    }

    public void runOnUiThread() {
        BaseApplication.mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isReadData) {
                    if (!isNullData) {
                        loadSuccessPager();
                    } else {
                        loadErrorPager();
                    }
                } else {
                    loadEmptyPager();
                }
            }
        }, 800);
    }

    private void loadNoNetwork() {
        if (mNoNetView == null) {
            mNoNetView = View.inflate(BaseApplication.mApp, R.layout.pager_no_net, null);
        }
        mNoNetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dynamic();
            }
        });
        changeView(mNoNetView);
    }

    private void loadProgressPager() {
        if (mProgressView == null) {
            mProgressView = View.inflate(BaseApplication.mApp, R.layout.pager_progress, null);
        }
        changeView(mProgressView);
    }

    private void loadEmptyPager() {
        if (mEmptyView == null)
            mEmptyView = View.inflate(BaseApplication.mApp, R.layout.pager_empty, null);
        mEmptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dynamic();
            }
        });
        changeView(mEmptyView);
    }


    private void loadErrorPager() {
        if (mErrorView == null)
            mErrorView = View.inflate(BaseApplication.mApp, R.layout.pager_error, null);
        mErrorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dynamic();
            }
        });
        changeView(mErrorView);
    }

    public void changeView(View view) {
        mRootView.removeAllViews();
        mRootView.addView(view);
    }
}
